import 'dart:developer';

import 'package:huxiaodui/background_run/bg_channel.dart';
import 'package:huxiaodui/api/model/alarm_model.dart';
import 'package:huxiaodui/api/model/mobile_app_model.dart';
import 'package:huxiaodui/utils/global_controller.dart';
import 'package:huxiaodui/main.dart';
import 'package:huxiaodui/components/alarm_dialog.dart';
import 'package:huxiaodui/view/index/map_page/map_controller.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_background_service/flutter_background_service.dart';

class PushApi {
  /// 单例模式
  static final PushApi _instance = PushApi._internal();
  factory PushApi() {
    return _instance;
  }
  PushApi._internal(){

    FlutterBackgroundService()
        .on("Push-Alarms")
        .listen((Map<String, dynamic>? event) async {
      log("前台处理 Push-Alarms");
      List<AlarmModel> alarm = AlarmModel.toList(event?["alarms"]);
    });

    FlutterBackgroundService()
        .on("Push-Alarm")
        .listen((Map<String, dynamic>? event) async {
      log("前台处理 Push-Alarm");
      AlarmDialog.show(event?["alarm"]);
    });

    FlutterBackgroundService().on("Push-MobileApp").listen((data) {
      log("前台处理 Push-MobileApp");
      List<dynamic> mobileApps = List<dynamic>.from(data?["mobileApps"]);
      MapController().mobileApps = [];
      for (var mobileApp in mobileApps) {
        mobileApp = MobileAppModel.fromJson(mobileApp);
        MapController().mobileApps.add(mobileApp);
      }
      MapController().updateMarkers();
      MapController().refresh();
    });

    FlutterBackgroundService().on("Push-MobileApp-Logout").listen((data) async {
      log("前台处理 Push-MobileApp-Logout");
      await showCupertinoDialog(
        barrierDismissible: false,
        context: navigatorKey.currentState!.context,
        builder: (BuildContext context) {
          return Container(
            color: Colors.black.withAlpha(90),
            child: CupertinoAlertDialog(
              title: const Text("下线通知"),
              content: const Text("您的账号在其他设备登录了！"),
              actions: [
                CupertinoDialogAction(
                  textStyle: TextStyle(
                      color: Theme.of(context).textTheme.bodyMedium?.color,
                      fontWeight: FontWeight.w700),
                  child: const Text('确定'),
                  onPressed: () async {
                    Navigator.pop(context);
                  },
                )
              ],
            ),
          );
        },
      );

      GlobalController().logout();
    });

    FlutterBackgroundService().on("Push-MobileApps").listen((data) async {
      log("前台处理 Push-MobileApps");
      List<dynamic> mobileApps = List<dynamic>.from(data?["mobileApps"]);
      MapController().mobileApps = [];
      for (var val in mobileApps) {
        var mobileApp = MobileAppModel.fromJson(val);
        MapController().mobileApps.add(mobileApp);
      }

      MapController().refresh();
    });
  }

  void send(String type,String? data) {
    FlutterBackgroundService().invoke(BgChannelType.mqttSend, {"type":type,"data":data});
  }
}
